﻿using System.Collections.Generic;
using HtmlAgilityPack;

namespace DeepMixWorkflow
{
    public class SelectedMusicParser
    {
        private readonly HtmlDocument _document;
        public SelectedMusicParser(string html)
        {
            _document = new HtmlDocument();
            _document.LoadHtml(html);
        }

        public IEnumerable<DeepMixTrack> GetSelectedTracks()
        {
            var aNodes = _document.DocumentNode.SelectNodes("//a[@class='playnow']");
            foreach (var aNode in aNodes)
            {
                var link = aNode.Attributes["href"].Value;
                var allText = aNode.SelectSingleNode("./strong").InnerText.Trim();
                var match = CommonHelper.TrackTitleRegex.Match(allText);
                var groups = match.Groups;
                var artistName = groups["artist"].Value;
                var trackName = groups["track"].Value;
                yield return new DeepMixTrack {Artist = artistName, Title = trackName, Link = link};
            }
        }
    }
}